package com.tripcube.system.controller;

import com.tripcube.common.core.web.controller.BaseController;
import com.tripcube.common.core.web.domain.AjaxResult;
import com.tripcube.common.core.web.page.TableDataInfo;
import com.tripcube.common.log.annotation.Log;
import com.tripcube.common.log.enums.BusinessType;
import com.tripcube.system.domain.SysUserFollows;
import com.tripcube.system.domain.vo.UserSimpleVO;
import com.tripcube.system.service.SysUserFollowsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/user/follows")
public class SysUserFollowsController extends BaseController {

    @Autowired
    private SysUserFollowsService userFollowsService;

    /**
     * 分页查询关注列表
     * @param follows 查询条件
     * @return 关注关系分页数据
     */
    @GetMapping("/list")
    public TableDataInfo list(SysUserFollows follows) {
        startPage();
        List<SysUserFollows> list = userFollowsService.selectUserFollowsList(follows);
        return getDataTable(list);
    }

    /**
     * 关注用户
     * @param followerId 关注者ID
     * @param followedId 被关注者ID
     * @return 是否关注成功
     */
    @PostMapping("/follow")
    @Log(title = "关注", businessType = BusinessType.INSERT)
    public AjaxResult follow(@RequestParam Long followerId, @RequestParam Long followedId) {
        boolean result = userFollowsService.follow(followerId, followedId);
        return AjaxResult.success("关注成功", result);
    }

    /**
     * 取消关注
     * @param followerId 关注者ID
     * @param followedId 被关注者ID
     * @return 是否取消成功
     */
    @PostMapping("/unfollow")
    @Log(title = "关注", businessType = BusinessType.DELETE)
    public AjaxResult unfollow(@RequestParam Long followerId, @RequestParam Long followedId) {
        boolean result = userFollowsService.unfollow(followerId, followedId);
        return AjaxResult.success("取消关注成功", result);
    }

    /**
     * 获取我关注的人ID集合
     * @param followerId 关注者ID
     * @return 关注的人ID集合
     */
    @GetMapping("/followedIds/{followerId}")
    public AjaxResult getFollowedIds(@PathVariable Long followerId) {
        Set<Long> followedIds = userFollowsService.getFollowedIds(followerId);
        return AjaxResult.success(followedIds);
    }

    /**
     * 获取我的粉丝ID集合
     * @param followedId 被关注者ID
     * @return 粉丝ID集合
     */
    @GetMapping("/followerIds/{followedId}")
    public AjaxResult getFollowerIds(@PathVariable Long followedId) {
        Set<Long> followerIds = userFollowsService.getFollowerIds(followedId);
        return AjaxResult.success(followerIds);
    }

    /**
     * 判断双方是否互相关注
     * @param uid1 用户1 ID
     * @param uid2 用户2 ID
     * @return 是否互相关注
     */
    @GetMapping("/isFollowEach")
    public AjaxResult isFollowEach(@RequestParam Long uid1, @RequestParam Long uid2) {
        boolean result = userFollowsService.isFollowEach(uid1, uid2);
        return AjaxResult.success(result);
    }

    /**
     * 获取关注数
     * @param followerId 关注者ID
     * @return 关注数量
     */
    @GetMapping("/countFollowed/{followerId}")
    public AjaxResult countFollowed(@PathVariable Long followerId) {
        long count = userFollowsService.countFollowed(followerId);
        return AjaxResult.success(count);
    }

    /**
     * 获取粉丝数
     * @param followedId 被关注者ID
     * @return 粉丝数量
     */
    @GetMapping("/countFollower/{followedId}")
    public AjaxResult countFollower(@PathVariable Long followedId) {
        Long count = userFollowsService.countFollower(followedId);
        return AjaxResult.success(count);
    }

    /**
     * 获取共同关注的人
     * @param uid1 用户1 ID
     * @param uid2 用户2 ID
     * @param limit 限制数量
     * @return 共同关注的人列表
     */
    @GetMapping("/commonFollows")
    public AjaxResult commonFollows(@RequestParam Long uid1, @RequestParam Long uid2, @RequestParam int limit) {
        List<UserSimpleVO> commonFollows = userFollowsService.commonFollows(uid1, uid2, limit);
        return AjaxResult.success(commonFollows);
    }
}